package com.xuecheng.teaching.controller;

import com.xuecheng.api.teaching.WorkApi;
import com.xuecheng.api.teaching.model.dto.WorkDTO;
import com.xuecheng.api.teaching.model.qo.QueryWorkModel;
import com.xuecheng.api.teaching.model.vo.WorkVo;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.teaching.common.util.UAASecurityUtil;
import com.xuecheng.teaching.convert.WorkConvert;
import com.xuecheng.teaching.service.WorkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 作业 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class WorkController implements WorkApi {


    @Autowired
    private WorkService  workService;


    /*
     *新增或修改作业
     */
    @PostMapping("/work")
    public WorkDTO createOrModifyWork(@RequestBody WorkVo workVo) {

        // 通过工具类获取机构ID（页面请求的视图数据里没有ID值）
        Long companyId = UAASecurityUtil.getCompanyId();

        //将vo转换为dto
        WorkDTO workDTO = WorkConvert.INSTANCE.vo2dto(workVo);
        //调用WorkService层新增或修改作业方法
        WorkDTO result =  workService.createOrModifyWork(workDTO,companyId);
        return result;
    }

    /**
     * 分页条件查询课程计划作业列表
     * @param params
     * @return
     */
    @PostMapping("/work/list")
    public PageVO queryWorkList(PageRequestParams params,String title, QueryWorkModel model) {

        //获得机构Id
        Long companyId = UAASecurityUtil.getCompanyId();
        //调service方法
        PageVO<WorkDTO> pageVO = workService.queryWorkList(params,title,companyId);
        return pageVO;
    }



    //   @DeleteMapping("teachplan/work/{teachplanWorkId}")
    //   public void removeWork(Long courseBaseId, @PathVariable Long teachplanWorkId) {
    //       Long companyId = UAASecurityUtil.getCompanyId();
    //       workService.removeWork(courseBaseId, teachplanWorkId,companyId);
    //   }

    @DeleteMapping("/work/{workId}")
    public void removeWork(@PathVariable Long workId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        workService.removeWork(workId, companyId);
    }

    /**
     * 根据Id查询媒资信息-远程调用
     * @param workId
     * @return
     */
    @GetMapping("l/work/{workId}")
    public RestResponse<WorkDTO> getWorkById4c(@PathVariable Long workId) {
        RestResponse<WorkDTO> response = workService.getWorkById4c(workId);
        return response;
    }



}
